package com.jack.payment.service.alipay.impl;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.jack.payment.entity.business.RequestPayBo;
import com.jack.payment.entity.generate.config.ConfigPayAliModel;
import com.jack.payment.entity.result.ResultPayVo;
import com.jack.payment.service.alipay.AliPaymentMethodService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;


@Service
public class AliPaymentMethodServiceImpl implements AliPaymentMethodService {
    private Logger log = LoggerFactory.getLogger(AliPaymentMethodServiceImpl.class);

    @Override
    public AlipayConfig setAliPaymentClientConfig(ConfigPayAliModel configPayAliModel) {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setCharset("utf-8");
        alipayConfig.setFormat("json");
        alipayConfig.setSignType("RSA2");
        alipayConfig.setAppId(configPayAliModel.getAppId());
        alipayConfig.setServerUrl(configPayAliModel.getGateway());
        alipayConfig.setPrivateKey(configPayAliModel.getPrivateKey());
        // 根据不同类型加载不同的支付配置类数据, 1: 密钥模式下 2: 证书模式下
        if (configPayAliModel.getType() == 1) {
            alipayConfig.setAlipayPublicKey(configPayAliModel.getAlipayPublicKey());
        } else if (configPayAliModel.getType() == 2) {
            alipayConfig.setAppCertContent(configPayAliModel.getAppCertContent());
            alipayConfig.setRootCertContent(configPayAliModel.getRootCertContent());
            alipayConfig.setAlipayPublicCertContent(alipayConfig.getAlipayPublicCertContent());
        }
        return alipayConfig;
    }

    @Override
    public ResultPayVo createAliQrcodePayInfo(RequestPayBo requestPayBo,
                                              String webhook,
                                              Integer payUseModule,
                                              AlipayClient client) throws AlipayApiException {
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();
        request.setNotifyUrl(webhook);
        model.setOutTradeNo(requestPayBo.getOrderNo());
        model.setTotalAmount(String.valueOf(requestPayBo.getAmount()));
        model.setSubject(requestPayBo.getDescribe());
        model.setBody(requestPayBo.getOrderNo());
        request.setBizModel(model);
        AlipayTradePrecreateResponse response = new AlipayTradePrecreateResponse();
        // 普通密钥模式
        if (payUseModule == 1) {
            response = client.execute(request);
            // 证书模式
        } else if (payUseModule == 2) {
            response = client.certificateExecute(request);
        }
        return new ResultPayVo().builder()
                .orderNo(requestPayBo.getOrderNo())
                .paymentSignature(response.getQrCode())
                .paymentAmount(requestPayBo.getAmount())
                .build();
    }

    @Override
    public ResultPayVo createAliWebPayInfo(RequestPayBo requestPayBo,
                                           String webhook,
                                           Integer payUseModule,
                                           AlipayClient client) throws AlipayApiException {
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        request.setNotifyUrl(webhook);
        model.setOutTradeNo(requestPayBo.getOrderNo());
        model.setTotalAmount(String.valueOf(requestPayBo.getAmount()));
        model.setSubject(requestPayBo.getDescribe());
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        model.setBody(requestPayBo.getOrderNo());
        request.setBizModel(model);
        AlipayTradePagePayResponse response = client.pageExecute(request);
        return null;
    }

    @Override
    public ResultPayVo createAliAppPayInfo(RequestPayBo requestPayBo,
                                           String webhook,
                                           Integer payUseModule,
                                           AlipayClient client) throws AlipayApiException {
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        request.setApiVersion("2.0");
        request.setNotifyUrl(webhook);
        model.setOutTradeNo(requestPayBo.getOrderNo());
        model.setTotalAmount(String.valueOf(requestPayBo.getAmount()));
        model.setSubject(requestPayBo.getDescribe());
        model.setBody(requestPayBo.getOrderNo());
        request.setBizModel(model);
        AlipayTradeAppPayResponse response = client.sdkExecute(request);
        return new ResultPayVo().builder()
                .orderNo(requestPayBo.getOrderNo())
                .paymentSignature(response.getBody())
                .paymentAmount(requestPayBo.getAmount())
                .build();
    }


}
